package org.snail.common.dingding.chat;

import com.dingtalk.open.client.ServiceFactory;
import com.dingtalk.open.client.api.model.corp.ChatInfo;
import com.dingtalk.open.client.api.model.corp.MessageBody;
import com.dingtalk.open.client.api.service.corp.ChatService;
import com.dingtalk.open.client.common.SdkInitException;
import com.dingtalk.open.client.common.ServiceException;
import com.dingtalk.open.client.common.ServiceNotExistException;

import java.util.List;

/**
 * 会话相关API
 */
public class ChatHelper {

    /**
     *  创建会话
     */
    public static String createChat(String accessToken, String name,
                                    String owner, List<String> useridlist) throws Exception {

        ChatService chatService = ServiceFactory.getInstance().getOpenService(ChatService.class);
        return chatService.createChat(accessToken, name, owner, useridlist);
    }


    /**
     * 更新会话
     */
    public static void updateChat(String access_token, String chatid, String name, String owner, List<String> add_useridlist, List<String> del_useridlist) throws Exception {
        ChatService chatService = ServiceFactory.getInstance().getOpenService(ChatService.class);
        chatService.updateChat(access_token, chatid, name, owner, add_useridlist, del_useridlist);
    }


    /**
     * 获取会话
     * @param id 指chatId
     */
    public static ChatInfo getChatInfo(String accessToken, String id)
            throws ServiceNotExistException, SdkInitException, ServiceException {
        ChatService chatService = ServiceFactory.getInstance().getOpenService(ChatService.class);
        ChatInfo chatInfo = chatService.getChat(accessToken, id);
        return chatInfo;
    }

    /**
     * 发送群消息
     */
    public static void  sendMessageToChat(String access_token, String chatid, String sender, String msgtype, MessageBody message)
            throws ServiceNotExistException, SdkInitException, ServiceException{
        ChatService chatService = ServiceFactory.getInstance().getOpenService(ChatService.class);
        chatService.sendMessageToChat(access_token, chatid, sender, msgtype, message);
    }

}
